var orderdata;
var orderpage;
var curhashpath;
var IPageNo=1;
 $(function(){
     loadorder(IPageNo);
     $("#tab div").click(function(){
         $("#tab div").removeClass("aui-active");
         $(this).addClass("aui-active");
         loadpage(1);
     });

    $(window).scroll(function(){
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight < scrollHeight - 100)
        {
            return;
        }
        IPageNo++;
        if(IPageNo<=orderpage){
            loadorder(IPageNo);
        }
    });
})
function loadpage(n) {
    var url=ctx+"/we/WeOrderlist?pno="+n+"&status="+$("#tab div.aui-active").attr("status");
    if(curhashpath)url+="#!"+curhashpath;
    window.location.href=url;
}
function loadorder(IPageNo){
    $w.httpRequest({
        url: apiroot + "/we/WeLoadorder",
        data: {
            "pno": IPageNo,
            "status":$("#tab div.aui-active").attr("status")
        },
        ok: function (ret) {
            orderdata = ret.Data.orderdata;
            orderpage = ret.Data.pagecount;
            if (orderdata.length == 0){
                emptyPage();

            } else {
                showOrder();
            }

        }
    })
};
function emptyPage() {
    var h2 = "";
    var stu = $(".my-order-list .aui-active").attr("status");

    $(".my-order-list").css("border-bottom","1px solid #dcdcdc");

    h2 += "<div class='empty-page'>";
    h2 += ("<span class='txt-kong-img'><img src=\"" + ctx + "/wesite/images/Thome.png\" ></span>");
    switch (stu){
        case '1':
            h2 += ("<p>没有待付款的订单</p>");
            break;
        case '3':
            h2 += ("<p>没有待发货的订单</p>");
            break;
        case '7':
            h2 += ("<p>没有待收货的订单</p>");
            break;
        case '11':
            h2 += ("<p>没有已完成的订单</p>");
            break;
        default:
            h2 += ("<p>您还没有购物记录</p>");
            break;
    }
    h2 += "</div>";
    $("#orderHolder").append(h2);
}
function showOrder(){
    var h='';
    var stua = orderdata.sta
    h=getOrderHtml();
    $("#orderHolder").append(h);
}
//订单列表
function getOrderHtml(){
    var html="<div class='tab-content'>";
    html+=template('orderHtml', {ctx:ctx,fileserver:fileserver,isuserauth:isuserauth,islogin:islogin,list:orderdata});
    html+="<div>";
    return html;
}
function shouhuo(ordercode){
	if(confirm("确定收货？")){
        $w.httpRequest({
            url:ctx+"/we/WeReceiptGoods",
            data:{"ordercode":ordercode},
            ok:function(){
                window.location.reload();
            }
        })
	}

}
function tuoihuo(ordercode){
	window.location.href=ctx+"/order/showbackwrite?ordercode="+ordercode;
}
function addOften(pid,obj){
	  $w.httpRequest({
		url:apiroot+"/order/addOften",
		data:{"priceid":pid},
		ok:function(ret){
			$(obj).text("已加入");
		}
	})
}
function cancelorder(ordercode){
	if(confirm("确定取消订单？")){
		$w.httpRequest({
			url:apiroot+"/we/WeCancelorder",
			data:{"ordercode":ordercode},
			ok:function(ret){
				window.location.reload();
			}
		})
	}
}
function buyagain(ordercode,id){
    var orderid = $("#"+id).attr("id");
    $("#"+orderid).attr("href","javascript:void(0);");
    setTimeout('$("#'+orderid+'").attr("href","javascript:buyagain(\''+ordercode+'\',\''+id+'\');");',2000);
    $w.httpRequest({
        url: apiroot + "/order/generateorder2",
        data: {"data": ordercode, "type": "orderbuyagain"},
        ok: function (ret) {
            var ordercode = ret.Data;
            window.location.href = ctx + "/we/order/"+ordercode;
        }, fail: function (ret) {
            if (ret.MsgCode == '101') {
                var h = "以下商品库存不足：";
                var list = window.eval(ret.MsgDesc);
                for (var i = 0; i < list.length; i++) {
                    h += "\r\n" + (i + 1) + "、" + list[i].productname + (list[i].attrname || '');
                }
                alert(h);
            } else if (ret.MsgCode == '102') {
                var h = "以下商品已下架：";
                var list = window.eval(ret.MsgDesc);
                for (var i = 0; i < list.length; i++) {
                    h += "\r\n" + (i + 1) + "、" + list[i].productname + (list[i].attrname || '');
                }
                alert(h);
            } else if (ret.MsgCode == '103') {
                var h = "以下商品已不在您的可购范围：";
                var list = window.eval(ret.MsgDesc);
                for (var i = 0; i < list.length; i++) {
                    h += "\r\n" + (i + 1) + "、" + list[i].productname + (list[i].attrname || '');
                }
                alert(h);
            } else alert(ret.MsgDesc);
            return false;
        }
    })

}